﻿Imports System
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.IO
Imports System.Xml.Serialization
Imports Microsoft.TeamFoundation.WorkItemTracking.Client
Imports System.Threading

Public Class ItemWrapper
    Implements INotifyPropertyChanged
    Implements IEquatable(Of ItemWrapper)

    Public Sub New(ByVal Node As Node)
        Me.m_Value = Node
        Me.m_Type = ItemType.Node
        Me.m_id = Node.Id.ToString
        Me.m_Name = Node.Name
    End Sub

    'Public Sub New(ByVal Project As Project)
    '    Me.m_Value = Project
    '    Me.m_Type = ItemType.Project
    '    Me.m_id = Project.Id.ToString
    '    Me.m_Name = Project.Name
    'End Sub

    Private m_id As String
    Private m_Name As String
    Private m_relationships As New ObservableCollection(Of Relationship)
    Private m_Type As ItemType
    Private m_WorkItems As WorkItemCollection

    Private m_Value As Node

    Public Property Value() As Node
        Get
            Return m_Value
        End Get
        Set(ByVal value As Node)
            m_Value = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the unique identifier for each Item.
    ''' </summary>
    Public Property Id() As String
        Get
            Return m_id
        End Get
        Set(ByVal value As String)
            If Not m_id = value Then
                m_id = value
                OnPropertyChanged("Id")
            End If
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the Name for each Item.
    ''' </summary>
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set(ByVal value As String)
            If Not m_Name = value Then
                m_Name = value
                OnPropertyChanged("Name")
            End If
        End Set
    End Property

    Public ReadOnly Property Type() As ItemType
        Get
            Return m_Type
        End Get
    End Property

    ''' <summary>
    ''' Collections of relationship connection for the Item
    ''' </summary>
    Public ReadOnly Property Relationships() As ObservableCollection(Of Relationship)
        Get
            Return m_relationships
        End Get
    End Property

    ''' <summary>
    ''' Accessor for the Item's children
    ''' </summary>
    Public ReadOnly Property Children() As Collection(Of ItemWrapper)
        Get
            Dim Selection As New Collection(Of ItemWrapper)

            For Each rel In Relationships
                If rel.RelationshipType = RelationshipType.Child Then
                    Selection.Add(rel.RelationTo)
                End If
            Next
            Return Selection
        End Get
    End Property

    ''' <summary>
    ''' Accessor for all of the Item's parents
    ''' </summary>
    Public ReadOnly Property Parents() As Collection(Of ItemWrapper)
        Get
            Dim selection As New Collection(Of ItemWrapper)

            For Each rel In Relationships
                If rel.RelationshipType = RelationshipType.Parent Then
                    selection.Add(rel.RelationTo)
                End If
            Next
            Return selection
        End Get
    End Property

    ''' <summary>
    ''' Accessor for the Item's siblings
    ''' </summary>
    Public ReadOnly Property Siblings() As Collection(Of ItemWrapper)
        Get
            Dim Selection As New Collection(Of ItemWrapper)
            For Each rel In Relationships
                If rel.RelationshipType = RelationshipType.Sibling Then
                    Selection.Add(rel.RelationTo)
                End If

            Next

            Return Selection
        End Get
    End Property

    ''' <summary>
    ''' Accessor for the item's spouses
    ''' </summary>
    Public ReadOnly Property Spouses() As Collection(Of ItemWrapper)
        Get
            Dim Selection As New Collection(Of ItemWrapper)
            ' TODO: Support Spouces

            Return Selection
        End Get
    End Property


#Region " INotifyPropertyChanged "

    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

    Public Sub OnPropertyChanged(ByVal PropertyName As String)
        If Not PropertyChangedEvent Is Nothing Then
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(PropertyName))
        End If
    End Sub

#End Region

#Region " IEquatable(Of TItem) "

    Public Shadows Function Equals(ByVal other As ItemWrapper) As Boolean Implements System.IEquatable(Of ItemWrapper).Equals
        Return other.Id = Me.Id
    End Function

#End Region

#Region "WorkItems"

    'Private Sub GetWorkItems_DoWork()
    '    '  Application.WorkItemStore.Query("")
    '    If Application.SelectedWITs.Count > 0 Then
    '        Dim qry As New System.Text.StringBuilder
    '        qry.Append("SELECT [System.Id], [System.Title] ")
    '        qry.Append("FROM WorkItems ")
    '        qry.Append("WHERE ")
    '        qry.Append("(")
    '        Dim IsFirst As Boolean = True
    '        For Each x In Application.SelectedWITs
    '            If Not IsFirst Then qry.Append("OR ")
    '            qry.Append("[System.WorkItemType] = 'Change Request'  ")
    '            IsFirst = False
    '        Next
    '        qry.Append(")  ")
    '        If Value.IsIterationNode Then
    '            qry.Append("AND  [System.IterationID] = '" & Value.Id & "' ")
    '        Else
    '            qry.Append("AND  [System.AreaID] = '" & Value.Id & "' ")
    '        End If
    '        qry.Append("ORDER BY [System.Id]")
    '        Debug.Write("TFS Query: " & qry.ToString)
    '        WorkItems = Application.SelectedProject.Store.Query(qry.ToString)
    '    End If
    'End Sub

#End Region


End Class
